﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Data;

namespace RCFlightTimer
{
	[Activity (Label = "Add", ParentActivity = typeof(MainActivity))]			
	public class AircraftActivity : Activity
	{
		private Aircraft selectedAircraft;
		private RCObject[] items;

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			SetContentView (Resource.Layout.Aircraft);

			newAircraftDialog ();

			aircraftTypeSpinner ();

			aircraftUpdateButton ();

			aircraftSpinner ();

			aircraftDeleteButton ();

			numberPickers ();
		}

		/*
		 * Sets up the dialog shown when the newAircraft button is pressed
		 * */
		private void newAircraftDialog(){
			//Show the new aircraft dialog when the button is pressed
			var newAircraftButton = FindViewById<Button> (Resource.Id.newAircraft);
			newAircraftButton.Click += (sender, e) => {
				var transaction = FragmentManager.BeginTransaction();
				var dialogFragment = new AddAircraftFragment();
				dialogFragment.Show(transaction, "dialog_fragment");
			};
		}

		/*
		 * Sets up the spinner for the aircraft type
		 * */
		private void aircraftTypeSpinner(){
			Spinner aircraftTypeSpinner = FindViewById<Spinner> (Resource.Id.typeSpinner);

			var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, Enum.GetNames(typeof(AircraftType)));

			aircraftTypeSpinner.Adapter = adapter;
		}

		/*
		 * Sets up the update button that saves any changes the user has made to the selected aircraft
		 * */
		private void aircraftUpdateButton(){
			var aircraftUpdateButton = FindViewById<Button> (Resource.Id.aircraftUpdateButton);
			aircraftUpdateButton.Click += (sender, e) => {
				if(selectedAircraft != null){
					//set the selected aircraft's fields to the values chosen by the user
					selectedAircraft.name = FindViewById<TextView> (Resource.Id.aircraftName).Text;
					selectedAircraft.description = FindViewById<TextView> (Resource.Id.aircraftDescription).Text;
					selectedAircraft.type = (AircraftType)Enum.Parse(typeof(AircraftType), FindViewById<Spinner> (Resource.Id.typeSpinner).SelectedItem.ToString());
					selectedAircraft.alarmMinutes = FindViewById<NumberPicker>(Resource.Id.minutePicker).Value;
					selectedAircraft.alarmSeconds = FindViewById<NumberPicker>(Resource.Id.secondPicker).Value;

					//update the aircraft in the database
					Database.Instance.AddOrUpdateAircraft(selectedAircraft);

					//Toast to let the user know it worked
					string toast = string.Format ("Saved data for {0}", selectedAircraft.name);
					Toast.MakeText (this, toast, ToastLength.Long).Show ();
				}
			};
		}

		/*
		 * Sets up the spinner for the selected aircraft
		 * */
		private void aircraftSpinner(){
			Spinner aircraftSpinner = FindViewById<Spinner> (Resource.Id.aircraftSpinner);
			aircraftSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (aircraftSpinnerItemSelected);

			var adapter = RCObjectAdapter.getAircraftAdapter (this, out items);

			aircraftSpinner.Adapter = adapter;
		}
		/*
		 * The method called when the user selectes an aircraft from the spinner
		 * */
		private void aircraftSpinnerItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
		{
			Spinner spinner = (Spinner)sender;

			//Get the selected aircraft from the list
			selectedAircraft = items [e.Position] as Aircraft;

			updateFieldsExceptSpinner ();
		}

		/*
		 * Sets the currently selected aircraft to aircraft
		 * */
		public void setSelected(Aircraft aircraft){
			selectedAircraft = aircraft;
		}

		/*
		 * Updates all of the fields visible on the screen
		 * */
		public void updateFields(){
			updateFieldsExceptSpinner ();

			//update the aircraft spinner
			Spinner aircraftSpinner = FindViewById<Spinner> (Resource.Id.aircraftSpinner);

			var adapter = RCObjectAdapter.getAircraftAdapter (this, out items);
			aircraftSpinner.Adapter = adapter;

			//Find the selected aircraft in the array by comparing the ids
			aircraftSpinner.SetSelection (Array.FindIndex(items, item => item.Id == selectedAircraft.Id));
		}

		/*
		 * Updates all of the fields visible on the screen except the aircraft spinner
		 * */
		public void updateFieldsExceptSpinner(){
			//update the fields
			FindViewById<TextView> (Resource.Id.aircraftName).Text = selectedAircraft.name;
			FindViewById<TextView> (Resource.Id.aircraftDescription).Text = selectedAircraft.description;

			Spinner aircraftTypeSpinner = FindViewById<Spinner> (Resource.Id.typeSpinner);

			aircraftTypeSpinner.SetSelection (Array.IndexOf (Enum.GetNames (typeof(AircraftType)), selectedAircraft.type.ToString()));
		}

		/*
		 * Deletes the currently selected aircraft
		 * */
		private void aircraftDeleteButton ()
		{
			var aircraftDeleteButton = FindViewById<Button> (Resource.Id.aircraftDeleteButton);
			aircraftDeleteButton.Click += (sender, e) => {
				if(selectedAircraft != null){
					Database.Instance.DeleteAircraft(selectedAircraft);

					updateFields();
				}
			};
		}

		void numberPickers ()
		{
			NumberPicker minutePicker = FindViewById<NumberPicker> (Resource.Id.minutePicker);
			NumberPicker secondPicker = FindViewById<NumberPicker> (Resource.Id.secondPicker);

			minutePicker.MaxValue = 60;
			minutePicker.MinValue = 0;

			secondPicker.MaxValue = 59;
			secondPicker.MinValue = 0;
		}
	}
}

